/**
* @file main.cpp
*
* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <iostream>
#include <pthread.h>
#include <dlfcn.h>
#include "sample_process.h"
#include "utils.h"
using namespace std;
bool g_isDevice = false;
bool thread_exit_flag = false;

static const char* DRV_SO_NAME = "libdrv_aicpu.so";
static const char* PROCESS_AICPU_FUNC_NAME="aclextProcessAicpuTask";
extern "C"{
void *aicpu_func_async_test(void *param) {
    void *aicpuSo=dlopen(DRV_SO_NAME, RTLD_LAZY);
    if(aicpuSo==NULL){
        ERROR_LOG("dlopen %s not found\n", DRV_SO_NAME);
        return NULL;
    }
    auto aclextProcessAicpuTask=(aclError(*)(int32_t))dlsym(aicpuSo, PROCESS_AICPU_FUNC_NAME);
    if(aclextProcessAicpuTask==NULL){
        ERROR_LOG("%s not found\n", PROCESS_AICPU_FUNC_NAME);
        dlclose(aicpuSo);
        return NULL;
    }
    while(thread_exit_flag == false) {
        aclextProcessAicpuTask(1000);
    }
    dlclose(aicpuSo);
    return NULL;
}
}

int main()
{
    SampleProcess sampleProcess;
    Result ret = sampleProcess.InitResource();
    if (ret != SUCCESS) {
        ERROR_LOG("sample init resource failed");
        return FAILED;
    }
    pthread_t g_id;
    pthread_create(&g_id, NULL, aicpu_func_async_test, NULL);
    ret = sampleProcess.Process();
    if (ret != SUCCESS) {
        ERROR_LOG("sample process failed");
        return FAILED;
    }
    thread_exit_flag = true;
    pthread_join(g_id, NULL);
    INFO_LOG("execute sample success");
    return SUCCESS;
}